package com.res.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.res.entity.Department;
import com.res.service.DepartmentService;
import com.res.util.FenyeUtil;

@RestController
@RequestMapping("/department")
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@GetMapping
public FenyeUtil<Department>findByMoHu(Department d,Integer page){
	FenyeUtil<Department> f=new FenyeUtil<Department>();
	Pageable pageable=PageRequest.of(page-1, 10);
	Page<Department> pd=departmentService.findByDname(d, pageable);
	int count=(int) pd.getTotalElements();
	f.setCode(0);
	f.setData(pd.getContent());
	f.setCount(count);
	return f;
}
@PostMapping
public Object AddDepart(Department d) {
	try {
		departmentService.AddandUpdateDepartment(d);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
@PutMapping
public Object UpDepart(Department d) {
	try {
		departmentService.AddandUpdateDepartment(d);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
@DeleteMapping
public Integer DeleteDepart(Integer did) {
	try {
		departmentService.deleteDepartment(did);
		return 1;
	} catch (Exception e) {
		// TODO: handle exception
		return 0;
	}
}
}
